package com.example.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.system.dto.SleepDuration;
import com.example.system.entity.Remind;
import com.example.system.entity.Sleep;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface SleepMapper extends BaseMapper<Sleep> {
    @Select("SELECT sleep_id, user_id, TIMESTAMPDIFF(SECOND, sleep_begin, sleep_end) / 3600.0 AS duration FROM sleep WHERE user_id = #{userId}")
    List<SleepDuration> findSleepByUserId(Integer userId);

    @Delete("DELETE FROM sleep WHERE sleep_id = #{sleepId}")
    int deleteSleepBySleepId(Integer sleepId);

    int addSleepBySleep(Sleep sleep);

    @Select("SELECT COUNT(*) > 0 FROM sleep " +
            "WHERE user_id = #{userId} AND sleep_id = #{sleepId};")
    boolean existsSleepKeyPair(@Param("userId") Integer userId,
                                @Param("sleepId") Integer sleepId);
}


